import AsyncStorage from "@react-native-async-storage/async-storage";
import axios, { AxiosResponse } from "axios";

const request = axios.create({
  baseURL: "http://192.168.124.37:3001/api",
  timeout: 10000, // 10秒超时
});

request.interceptors.request.use(async (config) => {
  const token = await AsyncStorage.getItem("token");
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

request.interceptors.response.use(
  (response): AxiosResponse<IResponse<any>> => {
    return response;
  },
  (error) => {
    return Promise.reject(error.response.data);
  }
);

export default request;
